/* ***************************************************
 * To Do List:
 * -------------------------------------
 *  -change the file size restriction to be set in the admin section, I don't like it being hard coded.
 *  -change the list that allows file to be dynamicly set not hard coded.
 * 
 * */



using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Admin_FileManagement : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        

    }// end of the page load method
    
    protected void butUpload_Click(object sender, EventArgs e)
    {


        Boolean booFileOK = false;
        String strPath = Server.MapPath("~/Files/");
        if (fulFile.HasFile)
        {
            // file size check - I want to change this to be set in the admin section some day
            if (fulFile.FileBytes.Length > 3145728)
            {
                labUploadMessage.Text = "Cannot accept files larger then 3mg.";
            }// end of the if that checks the file size
            else
            {
                String strFileExtension =
                    System.IO.Path.GetExtension(fulFile.FileName).ToLower();

                // I want to change this to be dynamic based on role and a list that 
                // that can be administered in the site not hard coded
                String[] strAllowedExtensions = { ".zip", ".rar", ".iso", ".img", ".nrg" };

                // check the allowed extentions
                for (int i = 0; i < strAllowedExtensions.Length; i++)
                {
                    if (strFileExtension == strAllowedExtensions[i])
                    {
                        booFileOK = true;
                    }// end of the check to see if this is a good extention

                }// end of the for loop

                // if we have a file and it is not good then it is a restricted type
                if (!booFileOK)
                {
                    labUploadMessage.Text = "Cannot accept files of this type.";
                }
                // do we have a file to save?
                else
                {
                    try
                    {
                        // upload the file 
                        fulFile.PostedFile.SaveAs(strPath
                            + fulFile.FileName);

                        // add an entry in the database 
                        FileManager.AddFile(fulFile.FileName, System.IO.Path.GetDirectoryName(strPath).ToLower(), tboDescription.Text.Trim(), strFileExtension, tboVersion.Text.Trim(), cboIsPublic.Checked);


                        // notify the use of success
                        labUploadMessage.Text = "File uploaded!";
                        
                    }
                    catch (Exception ex)
                    {
                        labUploadMessage.Text = "File could not be uploaded. <br>" + ex.Message;
                    }
                }// end of the if that deals with a good file to save
            }// end of the else section that controls the file upload after the size check
        }// end of the if that checks for a uploaded file           

        MultiView1.ActiveViewIndex = 1;
        //GridView1.DataBind();
    }// end of the butUpload_Click method   

    protected void ibuAdd_Click(object sender, ImageClickEventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
    }// end of the  method
    protected void ibuCancel_Click(object sender, ImageClickEventArgs e)
    {
        MultiView1.ActiveViewIndex = 1;
    }// end of the  method
    protected void ibuSave_Click(object sender, ImageClickEventArgs e)
    {
        Boolean booFileOK = false;
        String strPath = Server.MapPath("~/Files/");
        if (fulFile.HasFile)
        {
            // file size check - I want to change this to be set in the admin section some day
            if (fulFile.FileBytes.Length > 3145728)
            {
                labUploadMessage.Text = "Cannot accept files larger then 3mg.";
            }// end of the if that checks the file size
            else
            {
                String strFileExtension =
                    System.IO.Path.GetExtension(fulFile.FileName).ToLower();

                // I want to change this to be dynamic based on role and a list that 
                // that can be administered in the site not hard coded
                String[] strAllowedExtensions = { ".zip", ".rar", ".iso", ".img", ".nrg" };

                // check the allowed extentions
                for (int i = 0; i < strAllowedExtensions.Length; i++)
                {
                    if (strFileExtension == strAllowedExtensions[i])
                    {
                        booFileOK = true;
                    }// end of the check to see if this is a good extention

                }// end of the for loop

                // if we have a file and it is not good then it is a restricted type
                if (!booFileOK)
                {
                    labUploadMessage.Text = "Cannot accept files of this type.";
                }
                // do we have a file to save?
                else
                {
                    try
                    {
                        // upload the file 
                        fulFile.PostedFile.SaveAs(strPath
                            + fulFile.FileName);

                        // add an entry in the database 
                        FileManager.AddFile(fulFile.FileName, System.IO.Path.GetDirectoryName(strPath).ToLower(), tboDescription.Text.Trim(), strFileExtension, tboVersion.Text.Trim(), cboIsPublic.Checked);


                        // notify the use of success
                        labUploadMessage.Text = "File uploaded!";
                    }// tne of the try - file save 
                    catch (Exception ex)
                    {
                        labUploadMessage.Text = "File could not be uploaded. <br>" + ex.Message;
                    }// end of the catch - file save failed
                }// end of the if that deals with a good file to save
            }// end of the else section that controls the file upload after the size check
        }// end of the if that checks for a uploaded file           

        MultiView1.ActiveViewIndex = 1;
    }// end of the ibuSave_Click method
    protected void MultiView1_ActiveViewChanged(object sender, EventArgs e)
    {
        if (MultiView1.ActiveViewIndex == 1)
        {
            GridView1.DataBind();
        }// end of the check to see if this is the view with the grid
    }// end of the MultiView1_ActiveViewChanged method
}// end of the class
